﻿using UtilityExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Website.Logger;
using DAL;
using BOL;
using System.Web.Script.Services;
using System.Web.Services;
using WebLib;

namespace UILGestion.Admin.Obras
{
    public partial class ObraListForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    menuCtrl1.loadMenuSeguridad();
                    FormatForm();
                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);

                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }

            }
        }
      
        private void FormatForm()
        {



            ShowAllComboBox();
            

        }
        protected void ShowAllComboBox()
        {
            proyectoDropDownList.DataTextField = "descripcion";
            proyectoDropDownList.DataValueField = "idInternal";

            empresaDDL.DataTextField = "descripcion";
            empresaDDL.DataValueField = "idInternal";

            empresaDDL.DataSource=new ListValueBL().GetEmpresas();
            empresaDDL.DataBind();

            //List<ListValueInfo> proyectoList = new List<ListValueInfo>();
            //ListValueInfo proyecto = new ListValueInfo();
            
            //proyecto.idInternal = 0;
            //proyecto.descripcion= "-------------Todos -------------------";

            //proyectoList.Add(proyecto);
            //proyectoList.AddRange(p.ToList());

            //proyectoDropDownList.DataSource = proyectoList;
            //proyectoDropDownList.DataBind();

            



        }

        protected void proyectoDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                if (proyectoDropDownList.Items.Count > 0)
                {
                    int clase = int.Parse(proyectoDropDownList.SelectedValue);
                    string strclase = proyectoDropDownList.SelectedItem.Text;

                 
                    findValor.Value = strclase;
                    findIdValor.Value = clase.ToString();

                 
                    Find();



                }

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





            }
        }
     

        private void Find()
        {

            ////(DD/MM/yyyy)
            //System.IFormatProvider MiFp =
            //                                            new System.Globalization.
            //                                            CultureInfo("es-ES", false);
            ////valores de parametro
            //IQueryable<view_Obra> q;


            //    dbGestionDataContext _db;

            //    _db = new dbGestionDataContext(Parametros.strConnString);

                //int idEmpresa = (int)Session["login.Empresa.idEmpresa"];
                ////para todos los casos de  traer  todos  cuando escoje 0
                //// para cualquier nivel
                //if (findIdValor.Value == "0")
                //{
                //    q = from i in _db.view_Obra
                //        where i.idEmpresa==idEmpresa
                //        select i;

                    
                //}
                //else
                //{

                //        q = from i in _db.view_Obra
                //            where i.idProyecto == int.Parse(findIdValor.Value)
                //            select i;

                        

                //}

                //List<view_Obra> OL = new List<view_Obra>();

                //if (q.Count() == 0)
                //{
                //    view_Obra obraCero = new view_Obra();
                //    obraCero.idInternal = 0;

                //    OL.Add(obraCero);


                //}
                //else
                //{
                //    OL.AddRange(q.ToList());
                //}


                //OBRAGridView.DataSource = OL;
                //OBRAGridView.DataBind();

           
           
        }


        protected void nuevoButton_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("ObraEditForm.aspx");

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





            }
        }

        protected void actualizarButton_Click(object sender, EventArgs e)
        {
            try
            {
                Find();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





            }
        }

        private void  loadProyecto()
        {
             
            proyectoDropDownList.DataSource=new ListValueBL().GetProyectoComprasXEmpresa( empresaDDL.SelectedValue.ToInt());
                                proyectoDropDownList.DataBind();
        }

        protected void empresaDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                loadProyecto();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);



            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);





            }
        }

    }
}